const {
	$_ULOG,
} = require('../logger/logger')
const {
	getTime,
} = require('../../utils/dateUtils')
const {
	encPwd,
	funcVerify,
} = require('../utils')
const Response = require('./Response')
const roleDAO = require('../../utils/dao/role-dao')

module.exports = {

	Response,

	list: async function(req) {
		if (!funcVerify(req, 'role_list'))
			return new Response('201', '没有权限')
		let {
			role_ids,
		} = req.session._userInfo
		return roleDAO.list(req.body, role_ids)
	},

	add: async function(req) {
		if (!funcVerify(req, 'role_add'))
			return new Response('201', '没有权限')
		let role = await roleDAO.add(req.body)
		if (role == -1)
			return new Response('202', '角色ID已存在')
		if (role == -2)
			return new Response('202', '角色名已存在')
		return new Response('000', '成功', role)
	},

	modify: async function(req) {
		if (!funcVerify(req, 'role_modify'))
			return new Response('201', '没有权限')
		let role = await roleDAO.modify(req.body)
		if (!role || !role._id)
			return new Response('203', '修改失败')
		return new Response('000', '成功', role)
	},

	del: async function(req) {
		if (!funcVerify(req, 'role_del'))
			return new Response('201', '没有权限')
		await roleDAO.del(req.body)
		return new Response('000', '成功')
	},

	grantFuncs: async function(req) {
		if (!funcVerify(req, 'role_funcs'))
			return new Response('201', '没有权限')
		await roleDAO.grantFuncs(req.body)
		return new Response('000', '成功')
	},

	funcs: async function(req) {
		if (!funcVerify(req, 'role_funcs'))
			return new Response('201', '没有权限')
		let result = await roleDAO.funcs(req.body)
		return new Response('000', '成功', result)
	},
}